package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.user.dto.request.FaceIdVerifyReqDTO;
import com.niiwoo.civet.user.dto.request.FacePlusPortraitAuthRequestDTO;
import com.niiwoo.civet.user.dto.response.FacePlusPortraitAuthResponseDTO;
import com.niiwoo.civet.user.dto.response.FacePlusRespDTO;
import com.niiwoo.civet.user.enums.PortraitChannelEnum;
import com.niiwoo.civet.user.service.FacePlusPortraitAuthDubboService;
import com.niiwoo.civet.user.service.local.portraitAuth.FacePlusPortraitAuth;
import com.niiwoo.civet.user.service.local.portraitAuth.PortraitAuthOperation;
import com.niiwoo.civet.user.service.local.portraitAuth.PortraitAuthOperations;
import com.niiwoo.civet.user.service.local.portraitAuth.dto.FacePlusAuthInfoDTO;
import com.niiwoo.civet.user.service.local.portraitAuth.dto.FacePlusAuthResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

@Slf4j
@Service(version = "1.0.0")
public class FacePlusPortraitAuthDubboServiceImpl implements FacePlusPortraitAuthDubboService {

    PortraitAuthOperation portraitAuthOperation;

    @Autowired
    PortraitAuthOperations portraitAuthOperations;

    @Autowired
    FacePlusPortraitAuth facePlusPortraitAuth;

    @PostConstruct
    public void init() {
        portraitAuthOperation = portraitAuthOperations.getByChannel(PortraitChannelEnum.FACE_PLUS);
    }

    @Override
    public FacePlusRespDTO faceIdAuthVerify(FaceIdVerifyReqDTO reqDTO) {
        return facePlusPortraitAuth.faceIdAuthVerify(reqDTO);
    }

    @Override
    public FacePlusPortraitAuthResponseDTO saveAuthInfo(FacePlusPortraitAuthRequestDTO requestDTO) {
        log.info("face++保存信息###request={}", JSON.toJSONString(requestDTO));
        FacePlusAuthInfoDTO authInfoDTO = new FacePlusAuthInfoDTO();
        authInfoDTO.setUserId(requestDTO.getUserId());
        authInfoDTO.setLiveImageUrl(requestDTO.getLiveImageUrl());
        authInfoDTO.setFacePlusEnvImageUrl(requestDTO.getFacePlusEnvImageUrl());
        authInfoDTO.setDelta(requestDTO.getDelta());
        authInfoDTO.setChannel(PortraitChannelEnum.FACE_PLUS);
        portraitAuthOperation.saveAuthInfo(authInfoDTO);
        return this.wrapAuthResult(portraitAuthOperation.getAuthResult(requestDTO.getUserId(), true, FacePlusAuthResultDTO.class));
    }

    @Override
    public FacePlusPortraitAuthResponseDTO auth(FacePlusPortraitAuthRequestDTO requestDTO) {
        portraitAuthOperation.auth(requestDTO.getUserId());
        return this.wrapAuthResult(portraitAuthOperation.getAuthResult(requestDTO.getUserId(), true, FacePlusAuthResultDTO.class));
    }

    @Override
    public boolean invalidateUnAuthInfo(FacePlusPortraitAuthRequestDTO requestDTO) {
        return portraitAuthOperation.invalidateUnAuthInfo(requestDTO.getUserId());
    }

    @Override
    public FacePlusPortraitAuthResponseDTO getAuthInfo(FacePlusPortraitAuthRequestDTO requestDTO) {
        return this.wrapAuthResult(portraitAuthOperation.getAuthResult(requestDTO.getUserId(), false, FacePlusAuthResultDTO.class));
    }
    
    private FacePlusPortraitAuthResponseDTO wrapAuthResult(FacePlusAuthResultDTO authResult) {
        if (authResult == null) {
            return null;
        }
        FacePlusPortraitAuthResponseDTO responseDTO = new FacePlusPortraitAuthResponseDTO();
        responseDTO.setPortraitChannelEnum(authResult.getPortraitChannelEnum());
        responseDTO.setBestLiveImage(authResult.getBestLiveImage());
        responseDTO.setDelta(authResult.getDelta());
        responseDTO.setLiveEnvImage(authResult.getLiveEnvImage());
        responseDTO.setLiveImageMaxConfigConfidence(authResult.getLiveImageMaxConfigConfidence());
        responseDTO.setLiveImageMinConfigConfidence(authResult.getLiveImageMinConfigConfidence());
        responseDTO.setLiveImageConfidence(authResult.getLiveImageConfidence());
        responseDTO.setLiveImageThreshold1e3(authResult.getLiveImageThreshold1e3());
        responseDTO.setLiveImageThreshold1e4(authResult.getLiveImageThreshold1e4());
        responseDTO.setLiveImageThreshold1e5(authResult.getLiveImageThreshold1e5());
        responseDTO.setLiveImageThreshold1e6(authResult.getLiveImageThreshold1e6());
        responseDTO.setSyntheticFaceConfidence(authResult.getSyntheticFaceConfidence());
        responseDTO.setSyntheticFaceThreshold(authResult.getSyntheticFaceThreshold());
        responseDTO.setMaskConfidence(authResult.getMaskConfidence());
        responseDTO.setMaskThreshold(authResult.getMaskThreshold());
        responseDTO.setScreenReplayConfidence(authResult.getScreenReplayConfidence());
        responseDTO.setScreenReplayThreshold(authResult.getScreenReplayThreshold());
        responseDTO.setLiveImageAuthStatus(authResult.getLiveImageAuthStatus());
        responseDTO.setLiveAuthErrorCode(authResult.getLiveAuthErrorCode());
        responseDTO.setLiveAuthErrorMessage(authResult.getLiveAuthErrorMessage());
        responseDTO.setIdImage(authResult.getIdImage());
        responseDTO.setIdImageConfidence(authResult.getIdImageConfidence());
        responseDTO.setIdImageMaxConfigConfidence(authResult.getIdImageMaxConfigConfidence());
        responseDTO.setIdImageMinConfigConfidence(authResult.getIdImageMinConfigConfidence());
        responseDTO.setIdImageThreshold1e3(authResult.getIdImageThreshold1e3());
        responseDTO.setIdImageThreshold1e4(authResult.getIdImageThreshold1e4());
        responseDTO.setIdImageThreshold1e5(authResult.getIdImageThreshold1e5());
        responseDTO.setIdImageThreshold1e6(authResult.getIdImageThreshold1e6());
        responseDTO.setIdImageQuality(authResult.getIdImageQuality());
        responseDTO.setIdImageQualityThreshold(authResult.getIdImageQualityThreshold());
        responseDTO.setIdCardAttacked(authResult.getIdCardAttacked());
        responseDTO.setIdImageAuthStatus(authResult.getIdImageAuthStatus());
        responseDTO.setIdAuthErrorCode(authResult.getIdAuthErrorCode());
        responseDTO.setIdAuthErrorMessage(authResult.getIdAuthErrorMessage());
        responseDTO.setPortraitAuthStatus(authResult.getPortraitAuthStatus());
        responseDTO.setRemainingTimes(authResult.getRemainingTimes());
        return responseDTO;
    }

}
